/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd
 ******************************************************************************
 *  @File Name        : kf8a100fxx_spi.h
 *  @Author           : ChipON AE/FAE Group
 *  @Date             : 2023-12-19
 *  @HW Version       : KF8A100Fxx
 *  @Lib Version      : V2.0.1
 *  @Description      : This file provides the driver for the spi module
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd
 *  All rights reserved.
 *
 *  This software is copyright protected and proprietary to
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.
 *****************************************************************************/
/******************************************************************************
 *  |Date        |Version  |Author       |Description
 ******************************************************************************
 *  |2023-12-27  |V2.0     |Wang Junxiong|New creat
 *****************************************************************************/

#ifndef KF8A100FXX_SPI_H
#define KF8A100FXX_SPI_H

#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************
 *                      Include Files
 *****************************************************************************/
#include "kf8a100fxx.h"
/******************************************************************************
 *                      Macro or Inline
 *****************************************************************************/
/* 0 = Reception not completed, SSCIxBUFR empty (Receive mode)
 * 0 = Send complete, SSCIxBUFR empty (Transmit mode)
 * 1 = Receive completed, SSCIBUFR full (Receive mode)
 * 1 = Sending, SSCIBUFR full (Transmit mode) */
#define SPI_BUF_FULL_BIT SSCIBUF
/* SPI data receive buffer/transmit register */
#define SPI_BUFFER SSCIBUFR
/* SPI SCK release control bit*/
#define SPI_SCK_CTRL_BIT SSCICKP
/* SPI Receive overflow indicating bit*/
#define SPI_OVERFLOW_BIT SSCIOV

/**
 * @brief spi module disable/enable
 * 0: disable, 1:enable
 *
 */
#define SPI_ENABLE(x) (SSCIEN = (x))

/**
 * @brief timeout: time limit for flag query, Must not be less than SystemClock/4/BaudRate*8/9
 * You can set it larger if time permits, SystemClock/4/BaudRate*8/9*1.5
 * 0 ~ 65535
 *
 */
#define SPI_TIMEOUT_CONFIG (uint16_t)106u
/*****************************************************************************
 *                      Typedef Definitions
 *****************************************************************************/

/**
 * @brief spi mode and frequency division
 */
typedef enum
{
    SPI_MASTER_CLKDIV4  = 0u,
    SPI_MASTER_CLKDIV16 = 1u,
    SPI_MASTER_CLKDIV64 = 2u,
    SPI_SLAVE_SS_EN     = 4u,
    SPI_SLAVE_SS_DIS    = 5u
} Spi_ModeType;

/**
 * @brief Clock polarity select bit
 * 1 = In idle status, the clock is high
 * 0 = In idle status, the clock is low
 */
typedef enum
{
    /** @brief CKP = 0  */
    SPI_CPOL_0 = 0u,
    /** @brief CKP = 1  */
    SPI_CPOL_1 = 1u
} Spi_CpolModeType;

/**
 * @brief SPI clock edge select bit
 * 1 = Send data on the even edge of SCK, Sampling data on the odd edge of SCK
 * 0 = send data on the odd edge of SCK, Sampling data on the even edge of SCK
 */
typedef enum
{
    /** @brief CKE = 0  */
    SPI_CPHA_0 = 1u,
    /** @brief CKE = 1  */
    SPI_CPHA_1 = 0u
} Spi_CphaModeType;

/**
 * @brief SPI timeout error type.
 */
typedef enum
{
    SPI_OK,
    SPI_TIMEOUT
} Spi_StatusType;
/******************************************************************************
 *                      Export Variables
 *****************************************************************************/

/******************************************************************************
 *                      Export Functions
 *****************************************************************************/

/**
 * @brief SPI Initialization
 *
 * @param Mode spi mode and frequency division
 * @param Cpol Chip Select Active Polarity
 * @param Cpha Chip Select Active Polarity
 */
void Spi_Init(Spi_ModeType Mode, Spi_CpolModeType Cpol, Spi_CphaModeType Cpha);

/**
 * @brief SPI DeInitialization
 *
 */
void Spi_DeInit(void);

/**
 * @brief Enable or disable I2C/SPI/SSCIEN
 *
 * @param State 0: disable, 1: enable
 */
void Spi_Enable(uint8_t State);

/**
 * @brief SPI master sends one byte of data
 *
 * @param Data Send data
 * @return uint8_t receive data
 */
uint8_t Spi_MasterSendOneByteData(uint8_t Data);

/**
 * @brief SPI sends data of length Len
 *
 * @attention This function cannot be called within an interrupt service routine.
 *
 * @param TxData Send Data Address
 * @param RxData receive data address
 * @param Len lengths
 * @return Spi_StatusType
 * 0: SPI_OK,
 * 1: SPI_TIMEOUT,
 */
Spi_StatusType Spi_MasterTxRxData(const uint8_t *TxData, uint8_t *RxData, uint8_t Len);

/**
 * @brief SPI sends data of length Len
 *
 * @attention This function cannot be called within an interrupt service routine.
 *
 * @param TxData Send Data Address
 * @param Len lengths
 * @return Spi_StatusType
 * 0: SPI_OK,
 * 1: SPI_TIMEOUT,
 */
Spi_StatusType Spi_MasterTxData(const uint8_t *TxData, uint8_t Len);

/**
 * @brief SPI sends data of length Len
 *
 * @attention This function cannot be called within an interrupt service routine.
 *
 * @param TxData Send Data Address
 * @param RxData receive data address
 * @param Len lengths
 * @return Spi_StatusType
 * 0: SPI_OK,
 * 1: SPI_TIMEOUT,
 */
Spi_StatusType Spi_MasterRxData(uint8_t *RxData, uint8_t Len);
#ifdef __cplusplus
}
#endif

#endif
/* EOF */
